package cn.shop.lib_common;

import android.content.Context;
import android.graphics.Rect;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.widget.Toast;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;


import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


/**
 * Created by jian on 2016/4/7.
 */
public class CommonUtil {
    public static final String USER_HEAD_IMAGE_PATH = Environment.getExternalStorageDirectory()+
            File.separator+"Pictures"+File.separator+"head.png";
    public static final int LOGIN_AND_REGISTER_REQUEST_CODE = 10;
    public static final int LOGIN_AND_REGISTER_REGISTER_RESULT_CODE = 11;
    public static final int LOGIN_AND_REGISTER_LOGIN_RESULT_CODE = 12;
    public static final String CHAT_JID ="chat_jid" ;
    private static HanyuPinyinOutputFormat format;
    public static int getStatusBarHeight(View view){
        Rect rect=new Rect();
        view.getWindowVisibleDisplayFrame(rect);
        return rect.top;
    }

    public static String hanziTextToPinyinFormat(String text) {

        if (TextUtils.isEmpty(text)) return "";
        StringBuilder stringBuilder = new StringBuilder();
        char[] chars = text.toCharArray();

        for (char ch : chars) {
            if (Character.isWhitespace(ch)) continue;
            if (Character.toString(ch).matches("[\\u4E00-\\u9FA5]")) {
                stringBuilder.append(hanziToPinyin(ch));
            } else if (Character.isLetter(ch)) {
                stringBuilder.append(Character.toUpperCase(ch));
            } else {
                stringBuilder.append("#");
            }
        }
        return stringBuilder.toString();
    }

    public static String hanziToPinyin(char chinese) {
        if (format == null) {
            format = new HanyuPinyinOutputFormat();
            format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
            format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        }
        try {

            return PinyinHelper.toHanyuPinyinStringArray(chinese, format)[0];
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static Object evaluateColor(float fraction, Object startValue, Object endValue) {
        int startInt = (Integer) startValue;
        int startA = (startInt >> 24) & 0xff;
        int startR = (startInt >> 16) & 0xff;
        int startG = (startInt >> 8) & 0xff;
        int startB = startInt & 0xff;
        int endInt = (Integer) endValue;
        int endA = (endInt >> 24) & 0xff;
        int endR = (endInt >> 16) & 0xff;
        int endG = (endInt >> 8) & 0xff;
        int endB = endInt & 0xff;
        return (int) ((startA + (int) (fraction * (endA - startA))) << 24) |
                (int) ((startR + (int) (fraction * (endR - startR))) << 16) |
                (int) ((startG + (int) (fraction * (endG - startG))) << 8) |
                (int) ((startB + (int) (fraction * (endB - startB))));
    }

    public static Float evaluateFloat(float fraction, Number startValue, Number endValue) {

        float startFloat = startValue.floatValue();
        return startFloat + fraction * (endValue.floatValue() - startFloat);
    }

    private static ExecutorService sPool = Executors.newCachedThreadPool();
    private static Toast sToast;

    public static void runOnThread(Runnable task) {
    
        sPool.execute(task);
    }

    private static Handler sHandler = new Handler(Looper.getMainLooper());

    public static Handler getMainHandler() {
        return sHandler;
    }

    public static void runOnUIThread(Runnable task) {

        sHandler.post(task);
    }

    public static void showSafeToast(final Context context, final String text) {
        runOnUIThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
            }
        });
    }

    public static void showToast(Context context, String text) {
        Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
    }

    public static void showSingleToast(Context context, String text) {
        if (sToast == null) {
            sToast = Toast.makeText(context, text, Toast.LENGTH_LONG);
        }
        sToast.setText(text);
        sToast.show();
    }

    public static float dpToPx(Context context, float dp) {
         return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
    }
    public static String getCurrentTime() {
//        DateFormat dateFormat = SimpleDateFormat.getDateTimeInstance();

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(new Date());
    }
    public static String getChatHeadStr(String chatJid) {
        return String.format("与%1$s聊天中",chatJid);
    }
}
